package com.leno.mmall.controller.backend;

import com.leno.mmall.common.Const;
import com.leno.mmall.common.ResponseCode;
import com.leno.mmall.common.ServerResponse;
import com.leno.mmall.pojo.User;
import com.leno.mmall.service.*;
import com.leno.mmall.vo.CartVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/manage/category/")
public class CategoryManafeController {
    @Resource
    public IUserService iUserService;

    @Resource
    public ICategoryService iCategoryService;

//    1.获取品类子节点(平级)
    @RequestMapping("get_category.do")
    public ServerResponse getCategory(HttpSession session,Integer categoryId){
        User user = (User) session.getAttribute(Const.CURRENT_Admin);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录，请先登录");
        }
        if (iUserService.checkAdminRole(user).isSuccess()) {
            return iCategoryService.getChildrenParallelCategory(categoryId);
        } else {
            return ServerResponse.createByErrorMessage("无权限操作");
        }
    }

//####2.增加节点
@RequestMapping("add_category.do")
public ServerResponse addCategory(HttpSession session,Integer parentId,String categoryName){
    User user = (User) session.getAttribute(Const.CURRENT_Admin);
    if (user == null) {
        return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录，请先登录");
    }
    if (iUserService.checkAdminRole(user).isSuccess()) {
        return iCategoryService.addCategory(categoryName,parentId);
    } else {
        return ServerResponse.createByErrorMessage("无权限操作");
    }
}

//####3.修改品类名字

    @RequestMapping("set_category_name.do")
    public ServerResponse setCategoryName(HttpSession session,Integer categoryId,String categoryName){
        User user = (User) session.getAttribute(Const.CURRENT_Admin);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录，请先登录");
        }
        if (iUserService.checkAdminRole(user).isSuccess()) {
            return iCategoryService.updateCategoryName(categoryId,categoryName);
        } else {
            return ServerResponse.createByErrorMessage("无权限操作");
        }
    }

//    ####4.获取当前分类id及递归子节点categoryId

    @RequestMapping("get_deep_category.do")
    public ServerResponse getDeepCategory(HttpSession session,Integer categoryId){
        User user = (User) session.getAttribute(Const.CURRENT_Admin);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录，请先登录");
        }
        if (iUserService.checkAdminRole(user).isSuccess()) {
            return iCategoryService.selectCategoryAndChildrenById(categoryId);
        } else {
            return ServerResponse.createByErrorMessage("无权限操作");
        }
    }
}
